#include "process.h"

#define SIZE 101
#define STYLE '='

double SpeedFloat(double start, double range)
{
    int int_range = (int)range;
    return start + rand()%int_range + (range - int_range);
}

void FlushProcess(double curr, double total)
{
    static const char* lable = "|/-\\";
    static int index = 0;
    int len = strlen(lable);

    if (curr >= total) // 处理下载总量越界 
        curr = total;

    double rate = curr / total * 100;
    int cnt = (int)rate;
    char processbuff[SIZE];
    memset(processbuff, '\0', sizeof(processbuff));
    int i = 0;
    for (; i < cnt; i++)
    {
        processbuff[i] = STYLE;
    }

    // 刷新
    printf("[%-100s][%.1lf%%][%c]\r", processbuff, rate, lable[index++]); // lable闪动的次数只和函数调用次数相关 
    index %= len;
    fflush(stdout);
    usleep(3000);
    if (curr == total)
        printf("\n");
}
